package com.ying.tests.CCCbasis.Test4Thread;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Test4Runnable implements Runnable {

    private Thread thread;
    private String threadName;

    public Test4Runnable(String threadName) {
        this.threadName = threadName;
        System.out.println("Creating " + threadName);
    }

    @Override
    public void run() {
        System.out.println("Running " + threadName);

        try {
            for (int i = 4; i > 0; i --) {
                System.out.println("Thread " + threadName + "-" + i);
                Thread.sleep(50);
            }
        } catch (InterruptedException e) {
            System.out.println("Thread " + threadName + " interrupted.");
        }
        System.out.println("Thread " + threadName + " exiting.");
    }

    public void start() {
        System.out.println("Starting " + threadName);
        if (null == thread) {
            thread = new Thread(this, threadName);
            thread.start();
        }
    }

}
